package com.woniuxy.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.annotation.MultiRequestBody;
import com.woniuxy.exception.customException.TheAddedHasBeenExist;
import com.woniuxy.resultCode.Result;
import com.woniuxy.resultCode.ResultCode;
import com.woniuxy.system.entity.AssessmentIndicators;
import com.woniuxy.system.entity.IndiType;
import com.woniuxy.system.service.AssessmentIndicatorsService;
import com.woniuxy.system.service.IndiTypeService;
import com.woniuxy.system.view.AssessmentIndicatorView;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 考核指标 前端控制器
 * </p>
 *
 * @author woniuxy
 * @since 2022-06-01
 */
@RestController
@RequestMapping("/system/assessmentIndicators")
@Slf4j
@RequiresAuthentication
@Transactional
public class AssessmentIndicatorsController {
    @Autowired
    AssessmentIndicatorsService assessmentIndicatorsService;
    @Autowired
    IndiTypeService indiTypeService;
    /**
     * 获取所有的考核指标
     * @return
     */
    @PostMapping("/list")
    public Result list(@MultiRequestBody(required = false)AssessmentIndicatorView assessmentIndicatorView){
        if(assessmentIndicatorView.getCurrentPage()==null||assessmentIndicatorView.getPageSize()==null){
            List<AssessmentIndicators> list = assessmentIndicatorsService.list();
            return Result.ok().data("msg",list);
        }
        QueryWrapper<AssessmentIndicators> wrapper = getWrapper(assessmentIndicatorView);
        Page<AssessmentIndicators> page = new Page<>(assessmentIndicatorView.getCurrentPage(),assessmentIndicatorView.getPageSize());
        Page<AssessmentIndicators> result = assessmentIndicatorsService.getAssessmentIndicators(page,wrapper);
        List<AssessmentIndicators> records =  result.getRecords();
        long total = result.getTotal();
        long totalPages = result.getPages();
        return Result.ok().data("msg",records).data("total",total).data("totalPages",totalPages);
    }

    //封装queryWrapper
    private QueryWrapper<AssessmentIndicators> getWrapper(AssessmentIndicatorView assessmentIndicatorView) {
        QueryWrapper<AssessmentIndicators> queryWrapper = new QueryWrapper<>();
        if(assessmentIndicatorView!=null){
            if(StringUtils.isNotBlank(assessmentIndicatorView.getName())){
                queryWrapper.like("ai.name",assessmentIndicatorView.getName());
            }
            if(StringUtils.isNotBlank(assessmentIndicatorView.getAssessmentTypeId())){
                queryWrapper.eq("it.type_id",assessmentIndicatorView.getAssessmentTypeId());
            }

        }

        return queryWrapper;
    }


    /**
     * 添加考核指标
     * @param assessmentIndicators
     * @return
     */
    @PostMapping("/add")
    @RequiresRoles(value={"1","2","3"},logical = Logical.OR)
    public Result add(@MultiRequestBody AssessmentIndicators assessmentIndicators, @MultiRequestBody String userid ,@MultiRequestBody String AssessmentTypeId) throws TheAddedHasBeenExist {
        boolean flag = assessmentIndicatorsService.addAssessmentIndicators(assessmentIndicators,userid,AssessmentTypeId);

        if (flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    /**
     * 更新考核指标
     * @param assessmentIndicators
     * @return
     */
    @PostMapping("/update")
    @RequiresRoles(value={"1","2","3"},logical = Logical.OR)
    public Result update(@MultiRequestBody AssessmentIndicators assessmentIndicators, @MultiRequestBody String userid ,@MultiRequestBody String AssessmentTypeId){
        LambdaQueryWrapper<AssessmentIndicators> assessmentIndicatorsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        assessmentIndicatorsLambdaQueryWrapper.eq(AssessmentIndicators::getId,assessmentIndicators.getId());
        boolean update1 = assessmentIndicatorsService.update(assessmentIndicators, assessmentIndicatorsLambdaQueryWrapper);
        IndiType indiType = new IndiType();
        indiType.setIndiId(assessmentIndicators.getId());
        indiType.setTypeId(Long.valueOf(AssessmentTypeId));
        LambdaQueryWrapper<IndiType> indiTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        indiTypeLambdaQueryWrapper.eq(IndiType::getIndiId,indiType.getIndiId());
        boolean update = indiTypeService.update(indiType, indiTypeLambdaQueryWrapper);
        if (update1&&update){
            return  Result.ok().message("更新成功");
        }else {
            return Result.error().message("更新失败");
        }
    }

    /**
     * 根据指标id进行删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}/{indiId}")
    @RequiresRoles(value={"1","2","3"},logical = Logical.OR)
    public Result deleteById(@PathVariable Long id,@PathVariable Long indiId){
        boolean flag = assessmentIndicatorsService.removeById(id);
        QueryWrapper<IndiType> indiTypeQueryWrapper = new QueryWrapper<>();
        indiTypeQueryWrapper.eq("indi_id",indiId);
        boolean remove = indiTypeService.remove(indiTypeQueryWrapper);
        if (flag&&remove){
            return Result.ok().message("删除成功");
        }else {
            return Result.error().code(ResultCode.DELETE_ERROR.getCode()).message(ResultCode.DELETE_ERROR.getMessage());
        }
    }


}


